package com.gadgets.test.suite;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.context.ContextConfiguration;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2022-01-20
 */
@ContextConfiguration
@Configuration
public class GadgetsTestDbConf {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${spring.datasource.url}")
    private String gadgetDbUrl;
    @Value("${spring.datasource.username}")
    private String gadgetDbUserName;
    @Value("${spring.datasource.password}")
    private String gadgetDbPassword;
    @Value("${spring.datasource.driverClassName}")
    private String gadgetDbDriverClass;
    @Value("${spring.druid.initialSize}")
    private int gadgetsInitialSize;
    @Value("${spring.druid.minIdle}")
    private int gadgetsMinIdle;
    @Value("${spring.druid.maxActive}")
    private int gadgetsMaxActive;
    @Value("${spring.druid.maxWait}")
    private int gadgetsMaxWait;
    @Value("${spring.druid.timeBetweenEvictionRunsMillis}")
    private int gadgetsTimeBetweenEvictionRunsMillis;
    @Value("${spring.druid.minEvictableIdleTimeMillis}")
    private int gadgetsMinEvictableIdleTimeMillis;
    @Value("${spring.druid.testWhileIdle}")
    private boolean gadgetsTestWhileIdle;
    @Value("${spring.druid.testOnBorrow}")
    private boolean gadgetsTestOnBorrow;
    @Value("${spring.druid.testOnReturn}")
    private boolean gadgetsTestOnReturn;
    @Value("${spring.druid.keepAlive}")
    private boolean gadgetsKeepAlive;
    @Value("${spring.druid.poolPreparedStatements}")
    private boolean gadgetsPoolPreparedStatements;
    @Value("${spring.druid.maxPoolPreparedStatementPerConnectionSize}")
    private int gadgetsMaxPoolPreparedStatementPerConnectionSize;
    @Value("${spring.druid.filters}")
    private String gadgetsFilters;

    @Bean(name = "gadgetsDbTestSource")
    public DataSource gadgetsDataSource() throws SQLException {
        logger.info("注册gadgets-Test数据源");
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(gadgetDbDriverClass);
        druidDataSource.setUrl(gadgetDbUrl);
        druidDataSource.setUsername(gadgetDbUserName);
        druidDataSource.setPassword(gadgetDbPassword);
        druidDataSource.setInitialSize(gadgetsInitialSize);
        druidDataSource.setMinIdle(gadgetsMinIdle);
        druidDataSource.setMaxActive(gadgetsMaxActive);
        druidDataSource.setMaxWait(gadgetsMaxWait);
        druidDataSource.setTimeBetweenEvictionRunsMillis(gadgetsTimeBetweenEvictionRunsMillis);
        druidDataSource.setMinEvictableIdleTimeMillis(gadgetsMinEvictableIdleTimeMillis);
        druidDataSource.setTestWhileIdle(gadgetsTestWhileIdle);
        druidDataSource.setTestOnBorrow(gadgetsTestOnBorrow);
        druidDataSource.setTestOnReturn(gadgetsTestOnReturn);
        druidDataSource.setKeepAlive(gadgetsKeepAlive);
        druidDataSource.setPoolPreparedStatements(gadgetsPoolPreparedStatements);
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(gadgetsMaxPoolPreparedStatementPerConnectionSize);
        druidDataSource.setFilters(gadgetsFilters);
        return druidDataSource;
    }

    @Bean(name = "gadgetsTestDbTransactionManage")
    public DataSourceTransactionManager gadgetsDataSourceTransactionManager(@Qualifier("gadgetsDbTestSource") DataSource dataSource) throws SQLException {
        logger.info("注册事务管理器gadgetsTestDbTransactionManage");
        return new DataSourceTransactionManager(dataSource);
    }

}
